<template>
  <div>
    <el-form
      :model="jifen"
      :rules="rules"
      ref="jifen"
      label-width="100px"
      class="demo-ruleForm"
      style="margin: 30px"
    >
      <el-form-item label="借贷额度:" prop="borrowAmount">
        <el-input v-model="jifen.borrowAmount" style="width: 300px"></el-input>
        元
      </el-form-item>
      <el-form-item label="积分区间:" prop="integralStart">
        <el-input v-model="jifen.integralStart" style="width: 150px"></el-input>
        -
        <el-input v-model="jifen.integralEnd" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import integralGrade from '@/api/integralGrade'
export default {
  data() {
    return {
      jifen: {
        borrowAmount: '',
        integralStart: '',
        integralEnd: '',
      },
      rules: {
        integralStart: [
          { required: true, message: '请输入起始积分', trigger: 'blur' },
          { min: 3, max: 7, message: '长度在 1 到 7 ', trigger: 'blur' },
        ],
        integralEnd: [
          { required: true, message: '请输入结束积分', trigger: 'blur' },
          { min: 3, max: 7, message: '长度在 1 到 7 ', trigger: 'blur' },
        ],
        borrowAmount: [
          { required: true, message: '请输入借贷额度', trigger: 'blur' },
          { min: 3, max: 7, message: '长度在 3 到 7 ', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    //route 跳转到当前页面时的单个的路由对象
    // console.log(this.$route)
    // //获取路径中参数：
    // //路径参数： id    params
    // console.log('id: ' + this.$route.params.id)
    // //请求参数： t     query
    // console.log('t: ' + this.$route.query.t)
    //更新跳转过来时：页面初始化时异步加载页面中需要回显的数据
    if (this.$route.params.id) this.getById(this.$route.params.id)
  },
  methods: {
    getById(id) {
      integralGrade.getById(id).then((r) => {
        // console.log(r)
        this.jifen = r.data.item
      })
    },
    //表单提交按钮单击事件
    submitForm() {
      //判断是新增还是更新
      if (this.$route.params.id) {
        //更新
        integralGrade.update(this.jifen).then((r) => {
          this.$message(r.message)
          this.$router.push('/integral-grade/list')
        })
      } else {
        //新增
        integralGrade.save(this.jifen).then((r) => {
          this.$message(r.message)
          //跳转到积分等级列表页展示新增数据
          //必须通过路由路径来访问页面
          // window.location = 'http://localhost:9528/#/integral-grade/list'
          //通过路由去匹配传入的路径 匹配成功路由到目标页面
          this.$router.push('/integral-grade/list')
        })
      }
    },
  },
}
</script>
